using UnityEngine;
using System.Collections.Generic;

public class globalVarsClass : MonoBehaviour {

	public List<OTSprite> MobList = new List<OTSprite>(); //массив мобов в игре
   	public int MobCount = 0; //счетчик мобов в игре
	
	public List<OTSprite> bulletList = new List<OTSprite>(); //массив пуль в игре
	
	public List<OTSprite> enemyBulletList = new List<OTSprite>(); //массив пуль в игре
	
	public List<OTSprite> graphicList = new List<OTSprite>(); //массив графических элементов
	
	public List<OTSprite> locksList = new List<OTSprite>(); //массив замков в игре
   	public int locksCount = 0; //счетчик замков в игре
	
	public List<string> totalGemsTypesList = new List<string>(); // список камней в слотах пушки
	public List<int> curGemsAmountList = new List<int>(); // список камней в слотах пушки
	
	public List<string> gunGemsList = new List<string>(); // список камней в слотах пушки
	public int gunGemsCount = 0; //количество камней в слотах пушки
	public int gunState = 0; //текущее состояние орудия
	
	public List<string> shieldGemsList = new List<string>(); // список камней в слотах щита
	public int shieldGemsCount = 0; //количество камней в слотах щита
	public int shieldState = 0; //текущее состояние щита
	
	public int globalBonus = 0; // текущее состояние глобального бонуса
	
	public bool gameStopped = false; // пауза
	
	public bool gemIsDragging = false; // логическая переменная для камней
	
	public float width; // ширина сцены
	public float height; // высота сцены
	
	private backClass bc;
	public shieldClass shield;
	
	
	// Health parameters
	public float shieldHP;
	public float playerHP;      
	
	
	//удаление противников
	public void removeEnemy(int EnemyNumber){
		Destroy(MobList[EnemyNumber].gameObject);
		MobList.RemoveAt(EnemyNumber);
		MobCount -= 1;
	}
	
	//удаление снарядов игрока
	public void removeBullet(int BulletNumber){
		Destroy(bulletList[BulletNumber].gameObject);
		bulletList.RemoveAt(BulletNumber);
	}
	
	//удаление снарядов монстров
	public void removeEnemyBullet(int BulletNumber){
		Destroy(enemyBulletList[BulletNumber].gameObject);
		enemyBulletList.RemoveAt(BulletNumber);
	}
	
	//удаление графики
	public void removeGraphic(int graphicNumber){
		Destroy(graphicList[graphicNumber].gameObject);
		graphicList.RemoveAt(graphicNumber);
	}
	
	void Start () {
		height = 2 * Camera.main.orthographicSize;
	  	width = height * Camera.main.aspect;
		
		totalGemsTypesList.Add("red");
		curGemsAmountList.Add(0);
		
		totalGemsTypesList.Add("green");
		curGemsAmountList.Add(0);
		
		totalGemsTypesList.Add("blue");
		curGemsAmountList.Add(0);
		
		// reference to a shield
		shield = GameObject.Find("Shield").GetComponent<shieldClass>();
	}
	
	//считывание содержимого дырок
	public void refreshLocks(){
		gunGemsList.Clear();
		gunGemsCount = 0;
		shieldGemsList.Clear();
		shieldGemsCount = 0;
		for(int i = 0; i < locksList.Count; i++){
			if(locksList[i].GetComponent<lockClass>().lockType == "gun"){
				gunGemsList.Add(locksList[i].GetComponent<lockClass>().filledWith);
				gunGemsCount++;
			}else if(locksList[i].GetComponent<lockClass>().lockType == "shield"){
				shieldGemsList.Add(locksList[i].GetComponent<lockClass>().filledWith);
				shieldGemsCount++;
			}
		}
	}
	
	//установка глобального бонуса
	public void setGlobalBonus(string type){
		if(type == "red"){
			globalBonus = 1;
		}else if(type == "green"){
			globalBonus = 2;
		}else if(type == "blue"){
			globalBonus = 3;
		}else {
			globalBonus = 0;
		}
	}
	
	//установка бонуса щита
	public void refreshShieldBonuses(){
		refreshLocks();
		//Debug.Log(curGemsAmountList);
		//read currunt gems
		for(int j = 0; j < curGemsAmountList.Count; j++){
			curGemsAmountList[j] = 0;
		}
		for(int i = 0; i < shieldGemsList.Count; i++){
			for(int j = 0; j < totalGemsTypesList.Count; j++){
				if(shieldGemsList[i] == totalGemsTypesList[j]){
					curGemsAmountList[j]++;
				}
			}
		}
		//calculate bonuses
		if(curGemsAmountList[0] == 2){
			shieldState = 2;
			Debug.Log("2 red gemst");
			//2 red gems
		}else if(curGemsAmountList[1] == 2){
			shieldState = 4;
			Debug.Log("2 green gems");
			//2 green gems
		}else if(curGemsAmountList[2] == 2){
			shieldState = 6;
			Debug.Log("2 blue gems");
			//2 blue gems
		}else if(curGemsAmountList[0] == 1){
			if(curGemsAmountList[1] == 1){
				shieldState = 7;
				Debug.Log("1 red gem and 1 green");
				//1 red gem and 1 green
			}else if(curGemsAmountList[2] == 1){
				shieldState = 8;
				Debug.Log("1 red gem and 1 blue");
				//1 red gem and 1 blue
			}else{
				shieldState = 1;
				Debug.Log("1 red gem and 1 empty slot");
				//1 red gem and 1 empty slots
			}
		}else if(curGemsAmountList[1] == 1){
			if(curGemsAmountList[2] == 1){
				shieldState = 9;
				Debug.Log("1 green gem and 1 blue");
				//1 green gem and 1 blue
			}else{
				shieldState = 3;
				Debug.Log("1 green gem and 1 empty slots");
				//1 green gem and 1 empty slots
			}
		}else if(curGemsAmountList[2] == 1){
			shieldState = 5;
			Debug.Log("1 blue gem and 1 empty slot");
			//1 blue gem and 1 empty slot
		}else{
			Debug.Log("2 empty slots");
			shieldState = 0;
			//3 empty slots
		}
		
		shield.RefreshShieldBonusType();
	}
	
	//установка бонуса пушки
	public void refreshGunBonuses(){
		refreshLocks();
		//Debug.Log(curGemsAmountList);
		//read currunt gems
		for(int j = 0; j < curGemsAmountList.Count; j++){
			curGemsAmountList[j] = 0;
		}
		for(int i = 0; i < gunGemsList.Count; i++){
			for(int j = 0; j < totalGemsTypesList.Count; j++){
				if(gunGemsList[i] == totalGemsTypesList[j]){
					curGemsAmountList[j]++;
				}
			}
		}
		//calculate bonuses
		if(curGemsAmountList[0] == 3){
			gunState = 3;
			Debug.Log("3 red gems effect");
			//3 red gems effect	
		}else if(curGemsAmountList[1] == 3){
			gunState = 6;
			Debug.Log("3 green gems effect");
			//3 green gems effect	
		}else if(curGemsAmountList[2] == 3){
			gunState = 9;
			Debug.Log("3 blue gems effect");
			//3 blue gems effect	
		}else if(curGemsAmountList[0] == 2){
			if(curGemsAmountList[1] == 1){
				gunState = 11;
				Debug.Log("2 red gems and 1 green");
				//2 red gems and 1 green	
			}else if(curGemsAmountList[2] == 1){
				gunState = 15;
				Debug.Log("2 red gems and 1 blue");
				//2 red gems and 1 blue	
			}else{
				gunState = 2;
				Debug.Log("2 red gems and empty slot");
				//2 red gems and empty slot
			}
		}else if(curGemsAmountList[1] == 2){
			if(curGemsAmountList[0] == 1){
				gunState = 12;
				Debug.Log("2 green gems and 1 red");
				//2 green gems and 1 red	
			}else if(curGemsAmountList[2] == 1){
				gunState = 18;
				Debug.Log("2 green gems and 1 blue");
				//2 green gems and 1 blue	
			}else{
				gunState = 5;
				Debug.Log("2 green gems and empty slot");
				//2 green gems and empty slot
			}
		}else if(curGemsAmountList[2] == 2){
			if(curGemsAmountList[1] == 1){
				gunState = 17;
				Debug.Log("2 blue gems and 1 green");
				//2 blue gems and 1 green	
			}else if(curGemsAmountList[0] == 1){
				gunState = 14;
				Debug.Log("2 blue gems and 1 red");
				//2 blue gems and 1 red	
			}else{
				gunState = 8;
				Debug.Log("2 blue gems and empty slot");
				//2 blue gems and empty slot
			}
		}else if(curGemsAmountList[0] == 1){
			if(curGemsAmountList[1] == 1){
				if(curGemsAmountList[2] == 1){
					gunState = 19;
					Debug.Log("1 red gem, 1 green and 1 blue");
					//1 red gem, 1 green and 1 blue
				}else{
					gunState = 10;
					Debug.Log("1 red gem and 1 green");
					//1 red gem and 1 green
				}
			}else if(curGemsAmountList[2] == 1){
				gunState = 13;
				Debug.Log("1 red gem and 1 blue");
				//1 red gem and 1 blue
			}else{
				gunState = 1;
				Debug.Log("1 red gem and 2 empty slots");
				//1 red gem and 2 empty slots
			}
		}else if(curGemsAmountList[1] == 1){
			if(curGemsAmountList[2] == 1){
				gunState = 16;
				Debug.Log("1 green gem and 1 blue");
				//1 green gem and 1 blue
			}else{
				gunState = 4;
				Debug.Log("1 green gem and 2 empty slots");
				//1 green gem and 2 empty slots
			}
		}else if(curGemsAmountList[2] == 1){
			gunState = 7;
			Debug.Log("1 blue gem and 2 empty slots");
			//1 blue gem and 2 empty slots
		}else{
			gunState = 0;
			Debug.Log("3 empty slots");
			//3 empty slots
		}
	}
	
	//установка паузы
	public void setPause(bool pause){
		if (pause){
			gameStopped = true;
			bc = GameObject.Find("Background").GetComponent<backClass>();
			bc.setPause(true);
  
		}else{
			gameStopped = false;
			bc = GameObject.Find("Background").GetComponent<backClass>();
			bc.setPause(false);
		}
	}
}
